package com.api.controller;


import cn.hutool.crypto.digest.DigestUtil;
import com.api.annotation.Login;
import com.api.annotation.LoginUser;
import com.api.common.constant.Constant;
import com.api.common.page.PageData;
import com.api.common.utils.ConvertUtils;
import com.api.common.utils.Result;
import com.api.entity.Artcle;
import com.api.entity.Item;
import com.api.entity.User;
import com.api.entity.dto.ArtcleDTO;
import com.api.entity.dto.ItemDTO;
import com.api.entity.dto.UserDTO;
import com.api.service.IArtcleService;
import com.api.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-16
 */
@RestController
@RequestMapping("/api/artcle")
@AllArgsConstructor
@Tag(name = "文章管理")
public class ArtcleController {


    private final IArtcleService iArtcleService;

    /**
     * 分页
     */
    @Login
    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "title", description = "标题", in = ParameterIn.QUERY, ref = "String"),
    })
    public Result<PageData<ArtcleDTO>> page(@RequestParam Map<String, Object> params){
        PageData<ArtcleDTO> page = iArtcleService.page(params);
        return new Result<PageData<ArtcleDTO>>().ok(page);
    }


    /**
     * 详情
     */
    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<ArtcleDTO> get(@PathVariable("id") Long id) {
        Artcle item = iArtcleService.selectById(id);
        ArtcleDTO artcle = ConvertUtils.sourceToTarget(item, ArtcleDTO.class);
        return new Result<ArtcleDTO>().ok(artcle);
    }

    @Login
    @PostMapping
    @Operation(summary = "保存")
    public Result save(@RequestBody ArtcleDTO dto,@Parameter(hidden = true) @LoginUser User user) {
        Artcle item = ConvertUtils.sourceToTarget(dto, Artcle.class);
        item.setAuthor(user.getNickName());
        iArtcleService.insert(item);
        return new Result();
    }
    @Login
    @PutMapping("update")
    @Operation(summary = "修改")
    public Result update(@RequestBody ArtcleDTO dto,@Parameter(hidden = true) @LoginUser User user) {



        Artcle item = ConvertUtils.sourceToTarget(dto, Artcle.class);
        item.setAuthor(user.getNickName());
        iArtcleService.updateById(item);
        return new Result();
    }

    @Login
    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    public Result delete(@PathVariable("id") Long id) {
        iArtcleService.deleteById(id);
        return new Result();
    }

}
